package com.caifu.timer;

import com.caifu.mapper.BmmUserinfoMapper;
import com.caifu.mapper.CouponsRecordMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author tww
 * ClassName: Timer.java、
 * createTime: 2020年12月07日 14:45:45
 **/
@Configuration
@EnableScheduling
public class MyTimer {

    @Resource
    private BmmUserinfoMapper bmmUserinfoMapper;

    @Resource
    private CouponsRecordMapper couponsRecordMapper;

    /**
     * 添加定时任务 1.cron表达式格式：
     * {秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)}
     * <p>
     * 例 cron = "0 1 15 7 12 *"
     * 是指每年的12月7号15点1分执行
     */
    @Scheduled(cron = "59 59 23 31 12 *")
    private void configureTasks() {
        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
        /*QueryWrapper<BmUserinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        List<BmUserinfo> bmUserinfos = bmmUserinfoMapper.selectList(queryWrapper);
        if (bmUserinfos.size() > 0) {
            for (BmUserinfo bmUserinfo : bmUserinfos) {

                //重置优惠券
                bmUserinfo.setUnusedCoupons(12);
                bmUserinfo.setUsedCoupons(0);
                bmUserinfo.setTotalCoupons(12);
                bmmUserinfoMapper.updateById(bmUserinfo);

                //添加优惠券发放记录
                BmCouponsRecord bmCouponsRecord = new BmCouponsRecord();
                bmCouponsRecord.setBmUserinfoId(bmUserinfo.getId());
                bmCouponsRecord.setSourceUse("系统发放");
                bmCouponsRecord.setNumber("12");
                bmCouponsRecord.setType(bmUserinfo.getType());
                bmCouponsRecord.setHairCut("1");
                bmCouponsRecord.setCreatedTime(new Date());
                bmCouponsRecord.setUpdatedTime(new Date());
                couponsRecordMapper.insert(bmCouponsRecord);

            }
        }*/
    }

}
